package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.activity.NoAdoptActivity;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AdoptMdoel;
import com.banshuibao.bsb_android.util.DialogUtils;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.TimeUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/12/25.
 */

public class NoAdoptAdapter extends MineBaseAdapter<AdoptMdoel.DataBean> {

    private Context context;
    private NoAdoptActivity adoptActivity;

    public NoAdoptAdapter(Context context, List<AdoptMdoel.DataBean> data, int layoutResId) {
        super(context, data, layoutResId);
        this.context = context;
        adoptActivity = (NoAdoptActivity) context;
    }

    @Override
    protected void bindData(ViewHolder holder, AdoptMdoel.DataBean item) {
        ImageView headIv = (ImageView) holder.getView(R.id.head_iv_two);
        TextView name = (TextView) holder.getView(R.id.name_two);
        TextView position = (TextView) holder.getView(R.id.position_two);
        TextView content = (TextView) holder.getView(R.id.content_two);
        TextView date = (TextView) holder.getView(R.id.date_two);
        final TextView adopt = (TextView) holder.getView(R.id.status);
        final TextView adopt_two = (TextView) holder.getView(R.id.status_two);

        if (item != null) {
            String headImg = item.getHeadImg();
            String name1 = item.getName();
            String positiotn = item.getPositiotn();
            String duty = item.getDuty();
            String content1 = item.getContent();
            long ansDate = item.getAnsDate();
            final String uuid = item.getUuid();
            if (!TextUtils.isEmpty(headImg)) {
                ImageUtil.loadImageHead(context, headImg, headIv);
            }
            if (!TextUtils.isEmpty(name1)) {
                name.setText(name1);
            }
            if (!TextUtils.isEmpty(duty)) {
                position.setText(duty);
            } else {
                if (!TextUtils.isEmpty(positiotn)) {
                    position.setText(positiotn);
                }
            }
            content.setText(content1);
            String time = null;
            if (ansDate != 0) {
                time = TimeUtil.format(ansDate);
            } else {
                time = "";
            }
            date.setText(time);
            adopt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogUtils.getConfirmDialog(context, "您将采纳此答案", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            setAdoptPost(uuid);
                        }
                    }).show();
                }
            });
        }
    }

    private void setAdoptPost(String uuid) {
        AdoptMdoel.body body = new AdoptMdoel.body();
        body.setUuid(uuid);
        body.setType("1");
        HttpUtil.doPost(Constants.Url.ADOPT, body, new HttpResponse(context) {
            @Override
            public void onResult(Object obj) {
                String s = (String) obj;
//                LogUtil.e(s);
//                adopt.setVisibility(View.GONE);
//                adopt_two.setVisibility(View.VISIBLE);
//                adoptActivity.adapter.notifyDataSetChanged();
                adoptActivity.finish();
            }
        });
    }
}
